//
//  Person.m
//  objc_test
//
//  Created by Clover on 2021/7/20.
//

#import "Person.h"
#import <objc/runtime.h>

@implementation Person
+ (void)load
{
    NSLog(@"%s",__func__);
}

+ (void)initialize
{
    NSLog(@"%s",__func__);
}

- (void)yb_metalRun{
    NSLog(@"Person --> %s",__func__);
}

void YB_badAccessMethods(){
    NSLog(@"YB_badAccessMethods");
}

- (void)personRun{
    NSLog(@"Person --> %s",__func__);
}

// !!!: 动态方法解析
//+ (BOOL)resolveInstanceMethod:(SEL)sel{
//    NSLog(@"进来了");
//    if (sel == @selector(run)) {
//        return class_addMethod(self, @selector(run), YB_badAccessMethods, "V@:");
//    }
//    return  [super resolveInstanceMethod:sel];
//}

//+ (BOOL)resolveClassMethod:(SEL)sel{
//    if (sel == @selector(run)) {
//        return class_addMethod(self, @selector(run), YB_badAccessMethods, "V@:");
//    }
//    return [super resolveClassMethod:sel];
//}

/// !!!: 快速转发 -> 备用接收者
//- (id)forwardingTargetForSelector:(SEL)aSelector{
//    if (aSelector == @selector(run)) {
//        return [Son new];
//    }
//    return [super forwardingTargetForSelector:aSelector];
//}
//
//// !!!: 慢速转发
//- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
//    NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes:"V@:"];
//    return sig;
//}
//
//- (void)forwardInvocation:(NSInvocation *)anInvocation{
//    NSLog(@"forwardInvocation");
//    [anInvocation invokeWithTarget:[Son new]];
//}


- (void)printAllMethods{
    unsigned int count;
    
    Method *methods = class_copyMethodList([self class], &count);
    
    for (int i = 0; i < count; i++)
        
    {
        Method method = methods[i];
        
        SEL selector = method_getName(method);
        
        NSString *name = NSStringFromSelector(selector);
        
        //        if ([name hasPrefix:@"test"])
        
        NSLog(@"%@",name);
        
    }
}

@end

#pragma mark - Son
@interface Son()

@end

@implementation Son
- (void)run{
    [self class];
    NSLog(@"son run");
}
@end
 
